/*******************************************************************************
filename      NewLineSpaceCounter.cpp
author        Craig Williams

Brief Description:
  This driver simply counts all the new line and spaces that are found in a
    file. If a file is not specified, Parser.c is used.
*******************************************************************************/
#include "Parser.h"
#include <stdio.h>
#include <stdlib.h>

char *NewLineCounter(Parser &p, char *sp, int *);
char *SpaceCounter  (Parser &p, char *sp, int *);

int main(int argc, char *argv[])
{
  int    spaces   = 0;
  int    newlines = 1;
  char  *buffer;
  Parser p(0);

    /* Initalize the parser */
  if(argc == 1)
    p.LoadFile("Parser.cpp"); /* If no file is specified, assume Parser.cpp */
  else
    p.LoadFile(argv[1]);      /* Attempt to load what ever is specified.    */

  p.AddTokenSeparator("\n", 0, -1, 0, (PCBACK)NewLineCounter, &newlines);
  p.AddTokenSeparator(" " , 0, -1, 0, (PCBACK)SpaceCounter  , &spaces);

  for(buffer = p.GrabToken(); buffer; buffer = p.GrabToken())
    free(buffer);

  printf("New Lines : %d\n",  newlines);
  printf("Spaces    : %d\n",  spaces);
  printf("Characters: %ld\n", p.GetFilePosition());

  return 0;
}

char *NewLineCounter(Parser &p, char *sp, int *a)
{
  (*a)++;

  return sp;
}

char *SpaceCounter(Parser &p, char *sp, int *a)
{
  (*a)++;

  return sp;
}
